<?php

global $_W, $_GPC;
load()->func('tpl');
$op = empty($_GPC['op']) ? 'display' : $_GPC['op'];
if ($op == 'display') {
    $pindex = max(1, intval($_GPC['page']));
    $psize = 20;
    $lesson = pdo_fetchall('select * from ' . tablename('franchise_lesson') . "where  uniacid=:uniacid ORDER BY id DESC LIMIT " . ($pindex - 1) * $psize . ',' . $psize, array(':uniacid' => $_W['uniacid']));
    foreach ($lesson as $k => $v) {
        if($v['type']==1){
        $lesson[$k]['type']="线上课程";  
        }else{
        $lesson[$k]['type']="线下课程";     
        }
        $lesson[$k]['createtime'] = date('Y-m-d H:i:s', $v['createtime']);
        $lesson[$k]['starttime'] = date('Y-m-d H:i', $v['starttime']);
    }
    $total = pdo_fetchcolumn('select * from ' . tablename('franchise_lesson') . "where  uniacid=:uniacid ORDER BY id DESC", array(':uniacid' => $_W['uniacid']));
    $pager = pagination($total, $pindex, $psize);
    include $this->template('web/franchise/lesson');
    exit;
}
if ($op == 'status_set') {
    $id = intval($_GPC['id']);
    $info = pdo_fetch('select * from ' . tablename('franchise_lesson') . " where uniacid = '{$_W['uniacid']}' and id='{$id}' ");
    if (!empty($info)) {
        $status = $info['status'] == 1 ? '0' : '1';
        pdo_update("franchise_lesson", array('status' => $status), array("id" => $id, "uniacid" => $_W['uniacid']));
        die(json_encode(array("result" => 1, 'status' => $status)));
    } else {
        die(json_encode(array("result" => 0)));
    }
}
if ($op == "edit") {
    $id = intval($_GPC['id']);
    if (!empty($id)) {
        $lesson = pdo_fetch('SELECT * FROM ' . tablename('franchise_lesson') . ' WHERE id=:id and uniacid=:uniacid ', array(':uniacid' => $_W['uniacid'], ':id' => $id));
        $lesson['starttime'] = date('Y-m-d H:i', $lesson['starttime']);
    }
    if (array_key_exists('submit', $_GPC)) {
        empty($_GPC['type']) && message('请填写课程类型');
        empty($_GPC['title']) && message('请填写课程名称');
        empty($_GPC['address']) && message('请填写课程地址');
        empty($_GPC['starttime']) && message('请填写课程开始时间');
        empty($_GPC['thumb']) && message('请填写课程图片');
        $data['uniacid'] = $_W['uniacid'];
        $data['title'] = $_GPC['title'];
        $data['address'] = $_GPC['address'];
        $data['starttime'] = strtotime($_GPC['starttime']);
        $data['thumb'] = $_GPC['thumb'];
        $data['status'] = $_GPC['status'];
        $data['em_num'] = $_GPC['em_num'];
        $data['type'] = $_GPC['type'];
        $data['description'] = htmlspecialchars_decode($_GPC['description']);
        if (!empty($id)) {
            pdo_update('franchise_lesson', $data, array('uniacid' => $_W['uniacid'], 'id' => $id));
        } else {
            $data['createtime'] = time();
            pdo_insert('franchise_lesson', $data);
        }
        message('课程保存成功', $this->createWebUrl('franchise/lesson'), 'success');
    }
    include $this->template('web/franchise/lesson');
    exit;
}
if ($op == "join") {
    $meet_id = intval($_GPC['id']);
    if (empty($meet_id)) {
        message('课程不存在!', $this->createWebUrl('franchise/lesson'), 'error');
    }
    $pindex = max(1, intval($_GPC['page']));
    $psize = 20;
    $join_info = pdo_fetchall('select * from ' . tablename('franchise_lesson_join_info') . "as a left join " . tablename('ewei_shop_member') . " as m on a.openid=m.openid where a.uniacid=:uniacid and meet_id=:meet_id ORDER BY a.id DESC LIMIT " . ($pindex - 1) * $psize . ',' . $psize, array(':uniacid' => $_W['uniacid'], ':meet_id' => $meet_id));
    $total = pdo_fetchcolumn('select * from ' . tablename('franchise_lesson_join_info') . " as a left join " . tablename('ewei_shop_member') . " as m on a.openid=m.openid where a.uniacid=:uniacid and meet_id=:meet_id", array(':uniacid' => $_W['uniacid'], ':meet_id' => $meet_id));
    $pager = pagination($total, $pindex, $psize);
    include $this->template('web/franchise/lesson');
    exit;
}